﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text.RegularExpressions;

namespace IMDBApi.Contracts
{
    /// <summary>
    /// A class which provides all the functions to interact with the person features of the IMDb iPhone API.
    /// </summary>
    public class IMDBPerson : IMDBBase
    {
        /// <summary>
        /// A Person ID (nm0000000)
        /// </summary>
        protected string _personId;

        #region Constructor

        /// <summary>
        /// Constructor method.
        /// </summary>
        /// <param name="url">A IMDb URL (e.g. www.imdb.com/name/nm0000116/, nm0000116).</param>
        public IMDBPerson(string url)
            : base()
        {
            if (url == string.Empty)
            {
                throw new Exception(MethodInfo.GetCurrentMethod().Name + ": A IMDb URL is required.");
            }
            else
            {
                var personId = this.GetPersonIdFromUrl(url);
                this._personId = personId;
            }
        }

        #endregion

        #region Public

        /// <summary>
        /// Get a parsed person ID from an IMDb URL.
        /// </summary>
        /// <param name="url">an IMDb URL.</param>
        /// <returns>Returns parsed title ID.</returns>
        public string GetPersonIdFromUrl(string url)
        {
            Match match = Regex.Match(url, "nm[0-9]{7}", RegexOptions.IgnoreCase);

            if (match.Success)
            {
                return match.Value.ToString();
            }
            else
            {
                throw new Exception(MethodInfo.GetCurrentMethod().Name + ": Invalid person ID.");
            }
        }

        /// <summary>
        /// Return the current person ID.
        /// </summary>
        /// <returns>Returns current person ID.</returns>
        public string GetPersonId()
        {
            return this._personId;
        }

        /// <summary>
        /// Get a persons maindetails formatted as a JSON string.
        /// </summary>
        /// <returns>JSON formatted string.</returns>
        public string GetMainDetails()
        {
            Dictionary<string, string> arg = new Dictionary<string, string>() { { "nconst", this.GetPersonId() } };
            return this.MakeRequest("/name/maindetails", arg);
        }

        /// <summary>
        /// Get a persons photo urls formatted as a JSON string.
        /// </summary>
        /// <returns>JSON formatted string.</returns>
        public string GetPhotos()
        {
            Dictionary<string, string> arg = new Dictionary<string, string>() { { "nconst", this.GetPersonId() } };
            return this.MakeRequest("/name/photos", arg);
        }

        /// <summary>
        /// Get a persons filmography formatted as a JSON string.
        /// </summary>
        /// <returns>JSON formatted string.</returns>
        public string GetFilmography()
        {
            Dictionary<string, string> arg = new Dictionary<string, string>() { { "nconst", this.GetPersonId() } };
            return this.MakeRequest("/name/filmography", arg);
        }

        /// <summary>
        /// Get a persons trivia formatted as a JSON string.
        /// </summary>
        /// <returns>JSON formatted string.</returns>
        public string GetTrivia()
        {
            Dictionary<string, string> arg = new Dictionary<string, string>() { { "nconst", this.GetPersonId() } };
            return this.MakeRequest("/name/trivia", arg);
        }

        /// <summary>
        /// Get a persons quotes formatted as a JSON string.
        /// </summary>
        /// <returns>JSON formatted string.</returns>
        public string GetQuotes()
        {
            Dictionary<string, string> arg = new Dictionary<string, string>() { { "nconst", this.GetPersonId() } };
            return this.MakeRequest("/name/quotes", arg);
        }

        #endregion
    }
}